package vuze.rss;

import org.gudy.azureus2.plugins.Plugin;
import org.gudy.azureus2.plugins.PluginException;
import org.gudy.azureus2.plugins.PluginInterface;
import org.gudy.azureus2.plugins.UnloadablePlugin;
import org.gudy.azureus2.plugins.logging.LoggerChannel;
import org.gudy.azureus2.plugins.ui.UIInstance;
import org.gudy.azureus2.plugins.ui.UIManagerListener;
import org.gudy.azureus2.ui.swt.plugins.UISWTInstance;

import vuze.rss.ui.*;


public class RssPlugin implements Plugin, UnloadablePlugin
{
    // main widget instance
    private UISWTInstance mainInstance = null;
    private static final String VIEWID = "plugin.rssview.title";
    private static PluginInterface _pluginInterface;
    // main logging instance
    private static LoggerChannel logger;
    // pbject of main plugin widget
    private RssView rssView = null;
    // object of configuration widget
    private ConfigView configView = null;


    /**
     * Returns plugin interface
     *
     * @return PluginInterface
     */
    public static PluginInterface getPluginInterface()
    {
        return _pluginInterface;
    }

    /**
     * Returns logger channel
     *
     * @return LoggerChannel
     */
    public static LoggerChannel getLogger()
    {
        return logger;
    }

    public void initialize(final PluginInterface pluginInterface) throws
            PluginException
    {
//        try {
//            Class.forName("org.hsqldb.jdbc.JDBCDriver");
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
        _pluginInterface = pluginInterface;
        logger = pluginInterface.getLogger().getChannel(VIEWID);
        configView = ConfigView.getInstance();

        pluginInterface.getUIManager().addUIListener(new UIManagerListener()
        {
            public void UIDetached(UIInstance instance)
            {
                instance = null;
            }

            public void UIAttached(UIInstance instance)
            {
                if (instance instanceof UISWTInstance)
                {
                    rssView = new RssView();

                    mainInstance = ((UISWTInstance) instance);
                    mainInstance.addView(UISWTInstance.VIEW_MAIN, VIEWID, rssView);
                }
            }
        });
    }

    public void unload() throws PluginException
    {
        if(mainInstance != null)
        {
            mainInstance.removeViews(UISWTInstance.VIEW_MAIN, VIEWID);
        }
    }


}
